Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

428
Views
PHP -> Cómo sumar valores de matriz usando foreach

Soy nuevo desarrollador. Tengo un problema. Quiero la suma de todos los valores de calificación y luego se divide con 5. foreach show 325. Quiero el resultado 3+2+5=10 y luego 10/5=2

 foreach ($ratings as $ratingss) { echo $ratingss->rating ; }

volcado de var

 object(stdClass)[571] public 'id' => string '12' (length=2) public 'hotel_id' => string '37' (length=2) public 'rating' => string '3' (length=1) object(stdClass)[300] public 'id' => string '13' (length=2) public 'hotel_id' => string '37' (length=2) public 'rating' => string '5' (length=1)
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Puedes usar este código. Sumará todos los valores y los colocará en la variable $sum .

si su object(stdClass) -> entonces

 $sum = 0; // this is store all sum value so first assign 0 foreach ($ratings as $ratingss) { $sum += $ratingss->rating; // sum value with previous value and store it and no need to convert string type to int cause php do it } echo $sum; // this is final value echo $final_result = $sum / 5; // this is your desire result

si su matriz -> associative array entonces

 $sum = 0; // this is store all sum value so first assign 0 foreach ($ratings as $ratingss) { $sum += $ratingss['rating']; // sum value with previous value and store it and no need to convert string type to int cause php do it } echo $sum; // this is final value echo $final_result = $sum / 5; // this is your desire result

Espero que esto te ayudará

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!